package calorie.com.calorie.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.lxh.baselibray.view.PswText;
import com.lxh.baselibray.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.TransferContact;
import calorie.com.calorie.response.RealNameResponce;
import calorie.com.calorie.response.TransferAccountResponce;
import calorie.com.calorie.response.UserAssets;
import calorie.com.calorie.util.StaticValue;
import retrofit2.Response;


/**
 * 转帐
 * Created by ThinkPad on 2018-11-29.
 */

public class TransferActivity extends MvpActivity<TransferContact.TransferPresent> implements TransferContact.ITransferView {
    @BindView(R.id.tv_CurrencyType)
    TextView tv_CurrencyType;
    @BindView(R.id.titleView)
    TitleView titleView;
    @BindView(R.id.et_Account)
    EditText etAccount;
    @BindView(R.id.tv_Name)
    TextView tvName;
    @BindView(R.id.et_TransferNum)
    EditText etTransferNum;
    @BindView(R.id.tv_account)
    TextView tvAccount;


    String type = "CAL";
    @BindView(R.id.tv_currency_account_lay)
    LinearLayout tvCurrencyAccountLay;
    private boolean otherIsReal = false;//对方是否实名

    @Override
    public void initData(Bundle savedInstanceState) {
        etAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!ObjectUtils.isEmpty(s)) {
                    mPresent.getRealNameByPhone(context, s.toString());
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_transfer;
    }


    @OnClick({R.id.tv_CurrencyType, R.id.btn_confirm, R.id.tv_currency_account_lay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_currency_account_lay:
                //   String account = etAccount.getText().toString();
                showInputCalorieAccountDialog();
                break;
            case R.id.tv_CurrencyType:
                showTypeDialog();
                break;
            case R.id.btn_confirm:
                String account = etAccount.getText().toString();
                String money = etTransferNum.getText().toString();
                if (ObjectUtils.isEmpty(account)) {
                    ToastUtils.showLongToast(context, "帐户不能为空");
                    return;
                }
                if (ObjectUtils.isEmpty(money)) {
                    ToastUtils.showLongToast(context, "金额不能为空");
                    return;
                }
                if (!otherIsReal) {
                    ToastUtils.showLongToast(context, "对方未实名或帐户不存在");
                    return;
                }
                showDealDialog(account, money);
                break;
        }
    }

    AlertDialog accountDialog = null;

    private void showInputCalorieAccountDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()//默认弹窗动画
                .setCancelable(true)
                .setContentView(R.layout.dialog_input_accelerate_account)//载入布局文件
                .setWidthAndHeight(SizeUtils.dp2px(context, 300), SizeUtils.dp2px(context, 157))//设置弹窗宽高
                .setOnClickListener(R.id.tv_input_sure, v -> {//设置点击事件
                    EditText edInputAccount = accountDialog.getView(R.id.et_dialog_account);
                    String edDialogAccount = edInputAccount.getText().toString();
                    if (ObjectUtils.isEmpty(edDialogAccount)) {
                        ToastUtils.showLongToast(context, "账号不能为空");
                        return;
                    }
                    etAccount.setText(edDialogAccount);
                    tvAccount.setText(edDialogAccount);
                    accountDialog.dismiss();
                }).setOnClickListener(R.id.tv_input_cancel, v -> accountDialog.dismiss());
        accountDialog = builder.create();
        accountDialog.show();

    }

    AlertDialog dealDialog = null;

    private void showDealDialog(String account, String tokenNum) {//交易密码弹窗
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_deal_pwd)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    PswText psw = dealDialog.getView(R.id.pt_deal_pwd);
                    String dealPwd = psw.getPsw();
                    String toName = tvName.getText().toString();
                    mPresent.transfer(context, type, Double.valueOf(tokenNum), dealPwd, toName, account);
                    dealDialog.dismiss();

                }).setOnClickListener(R.id.tv_cancle, v -> dealDialog.dismiss());
        dealDialog = builder.create();
        dealDialog.show();
    }

    AlertDialog typeDialog = null;

    public void showTypeDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_type_select_money)
                .fromBottom(true)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 130))
                .setOnClickListener(R.id.cal_lay, v -> {
                    RadioButton radioCal = typeDialog.getView(R.id.radio_cal);
                    RadioButton radioInvite = typeDialog.getView(R.id.radio_invite);
                    radioCal.setChecked(true);
                    radioInvite.setChecked(false);
                    tv_CurrencyType.setText("CAL");
                    type = "CAL";
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.invite_lay, v -> {
                    RadioButton radioCal = typeDialog.getView(R.id.radio_cal);
                    RadioButton radioInvite = typeDialog.getView(R.id.radio_invite);
                    radioCal.setChecked(false);
                    radioInvite.setChecked(true);
                    tv_CurrencyType.setText("激活券");
                    type = "VIT";
                    typeDialog.dismiss();
                }).setOnClickListener(R.id.tv_select_sure, v -> typeDialog.dismiss());
        typeDialog = builder.create();
        typeDialog.show();

    }

    //转账成功弹窗
    AlertDialog successDialog = null;

    public void successTipDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_success)
                .setWidthAndHeight(SizeUtils.dp2px(context, 100), SizeUtils.dp2px(context, 107))
                .setText(R.id.tx_tip, "转账成功");
        successDialog = builder.create();
        successDialog.show();
    }

//    public void showTypeDialog() {//币种选择弹窗
//        AlertDialog.Builder builder = new AlertDialog.Builder(context)
//                .addDefaultAnimation()
//                .setCancelable(true)
//                .setText(R.id.tv_one, "CAL")
//                .setText(R.id.tv_two, "邀请券")
//                .setContentView(R.layout.dialog_type_select_money)
//                .fromBottom(true)
//                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(context, 130))
//                .setOnClickListener(R.id.tv_one, v -> {
//                    type = "CAL";
//                    tv_CurrencyType.setText("邀请券");
//                    typeDialog.dismiss();
//                }).setOnClickListener(R.id.tv_two, v -> {
//                    type = "邀请券";
//                    tv_CurrencyType.setText("邀请券");
//                    typeDialog.dismiss();
//                });
//        typeDialog = builder.create();
//        typeDialog.show();
//    }

    @Override
    public void getTransferData(Response<TransferAccountResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            successTipDialog();
//            ToastUtils.showLongToast(context, "转帐成功");
            StaticValue.userAssets = new Gson().fromJson(new Gson().toJson(response.body().getData()), UserAssets.class);
            finish();
            return;
        }
        ToastUtils.showLongToast(context, response.body().getMsg());
    }

    @Override
    public void getRealNameByPhoneData(Response<RealNameResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            otherIsReal = true;
            tvName.setTextColor(Color.parseColor("#ffb4b4b4"));
            tvName.setText(response.body().getData());
        } else if (response.body().getCode() == 202) {
            tvName.setTextColor(Color.RED);
            tvName.setText("对方没有实名认证");
        } else if (response.body().getCode() == 203) {
            tvName.setTextColor(Color.RED);
            tvName.setText("账户不存在");
        } else {
            tvName.setTextColor(Color.RED);
            tvName.setText("系统错误");
        }
    }

    @Override
    protected TransferContact.TransferPresent createPresent() {
        return new TransferContact.TransferPresent();
    }

}
